<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
header('Content-type: text/JavaScript');
//header('Content-type: image/gif');
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');

//include_once("baja.common.php");
function req($nm, $default) {
	$v = $_POST[$nm];
	if ($v == 'TRUE' || $v == 'true')
		return TRUE;
	if ($v == 'FALSE' || $v == 'false')
		return FALSE;
	if ($v) {
		if (0 === strpos($v, 'transfer/double-encoded:'))
			return pureTx(rawurldecode(substr($v, 24)));
		return pureTx($v);
	}
	$v = $_GET[$nm];
	if ($v == 'TRUE' || $v == 'true')
		return TRUE;
	if ($v == 'FALSE' || $v == 'false')
		return FALSE;
	if ($v)
		return pureTx($v);
	return $default;
}

function pureTx($tx) {
	if (get_magic_quotes_gpc() == 0)
		return $tx;
	return stripslashes($tx);
}

// Input
//$file = req('file', '');
//$path = req('path', '');

$file = "yoshi1.gif";
$path = '';

$key = req('key');
$callbackName = req('callback', FALSE);

$result = TRUE;
$ot = $aot = '';
$content = '';
$isFolder = FALSE;
$exists = FALSE;
$size = 0;
$msg = 0;
$fv = 0;
$reload = FALSE;
$lastChanged = 0;

//$fullPath = webPath($path, $file);
$fullPath = $file;

//if ($validCredentials !== TRUE) {
//	if ($callback) {
//		$ee = '';
//		$ee .= "$callbackName({\n";
//		$ee .= "result: false,\n";
//		$ee .= "msg: \"invalid credentials\",\n";
//		$ee .= "end: true\n";
//		$ee .= "});";
//		echo $ee;
//	}
//	exit(0);
//}

$pi = pathinfo($fullPath);
$minkey = $pi['dirname'] . '/.@@js';
$minname = $pi['dirname'] . '/.' . $pi['basename'];

if (file_exists($minkey) && file_exists($minname))
	$fullPath = $minname;

if (file_exists($fullPath)) {
	$exists = TRUE;
	if (is_dir($fullPath)) {
		$result = FALSE;
		$isFolder = TRUE;
		$msg = 'requested content of a folder, not a file';
	} else {
		$content = file_get_contents("yoshi1.gif");
		//$content = file_get_contents("fb.photo.php");
		$size = strlen($content);
		if ($reload === TRUE && $size > 40) {
			file_put_contents($fullPath, $content, LOCK_EX);
		}
		clearstatcache();
		$lastChanged = filemtime($fullPath);
	}
} else {
	$result = FALSE;
	$msg = 'file does not exist';
}

echo $content;
//echo base64_encode($content);
exit(0);

if ($callbackName)
	$ot .= "$callbackName({\n";
else
	$ot .= "{\n";
$ot .= '"result":' . json_encode($result) . ",\n";
$ot .= '"file":' . json_encode($file) . ",\n";
$ot .= '"path":' . json_encode($path) . ",\n";
$ot .= '"fullPath":' . json_encode($fullPath) . ",\n";
$ot .= '"lastChanged":' . json_encode($lastChanged) . ",\n";
$ot .= '"cwd":' . json_encode(getcwd()) . ",\n";
$ot .= '"root":' . json_encode($_ENV['DOCUMENT_ROOT']) . ",\n";
$ot .= '"msg":' . json_encode($msg) . ",\n";
$ot .= '"key":' . json_encode($key) . ",\n";
$ot .= '"size":' . json_encode($size) . ",\n";
$ot .= '"host":' . json_encode($_ENV['SERVER_NAME']) . ",\n";
$ot .= '"exists":' . json_encode($exists) . ",\n";
$ot .= '"padtype":' . json_encode($padtype) . ",\n";
$ot .= '"fv":' . json_encode($fv) . ",\n";
$ot .= '"reload":' . json_encode($reload) . ",\n";
$ot .= '"content64":' . json_encode(base64_encode($content)) . ",\n";
//$ot .= '"content64":' . base64_encode($content) . ",\n";
if ($callbackName)
	$ot .= "})";
else
	$ot .= "}";
echo $ot;
?>
